<template>
  <div class="login">
    <div class="box">
      <div class="img-box"><img src="../assets/logo.png" alt=""  class="img2"/></div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="ruleForm.username"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="ruleForm.password"
            prefix-icon="el-icon-unlock"
          ></el-input>
        </el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
      </el-form>
    </div>
  </div>
</template>
<script>
import { GetLogin } from "../utils/api";
import { mapMutations } from 'vuex'
export default {
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //登录
    ...mapMutations(['setLogin']),
     login() {
      clearTimeout(this.timer)
     this.timer =  setTimeout(async()=>{
       if (this.ruleForm.username == "" || this.ruleForm.password == "") {
        this.$message.error("内容不能为空");
        return false;
      }

      var data = await GetLogin(this.ruleForm);
      console.log(data.data.data.token,99);

      if (data.data.meta.status == 200) {
        // console.log(1);
        // 存储到本地
        // localStorage.setItem("token", JSON.stringify(data.data.data.token));
        // 映射函数调用
        this.$message.success(data.data.meta.msg)
         this.setLogin(data.data.data.token)
        this.$router.push("/index");
      }
    },300)
    },
  },

  
};
</script>
<style lang="scss">
.box {
  margin: auto;
  width: 400px;
  height: 400px;
  box-shadow: 0 0 15px rgb(231, 231, 231);
  margin-top: 100px;
  position: relative;
}
.img-box {
  width: 140px;
  height: 140px;
  padding: 10px;
  box-shadow: 0 0 10px #ccc;
  border-radius: 50%;
  position: absolute;
  left: 120px;
  top: -80px;
}
.img2 {
  width: 140px;
  height: 140px;
  background: #ccc;
  border-radius: 50%;
}

.el-form {
  padding-top: 150px;
  padding-right: 30px;
}
.el-button {
  width: 100px;
  margin-left: 150px;
}


</style>